package com.zlq.ajaxjson.controller;

import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/loginController")
public class LoginController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");


        String name = req.getParameter("name");
        String password = req.getParameter("password");
        PrintWriter out = resp.getWriter();
        JSONObject json = new JSONObject();
        if ("admin".equals(name) && "123".equals(password)){
            json.put("result",true);
            json.put("loginForm","登录成功");
//            req.getRequestDispatcher("index.jsp").forward(req,resp);
//            resp.sendRedirect("index.jsp");
        }else {
            json.put("result",false);
            json.put("loginForm","用户名或密码错误");
//            resp.sendRedirect("login.jsp");
        }
        out.write(json.toString());
        out.flush();
        out.close();

    }
}
